package site.wetsion.campusgrade.codeimg;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

//import org.json.JSONObject;

import com.asprise.ocr.Ocr;
//import com.baidu.aip.ocr.AipOcr;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import site.wetsion.campusgrade.base64.BASE64;


public class CodeDiscern {  
 
	//设置APPID/AK/SK
    public static final String APP_ID = "你的 App ID";
    public static final String API_KEY = "a9e80e65b4f94017a8317e877a14f34e";
    public static final String SECRET_KEY = "1efcf9bc51d748dc899e06758f8bd6f8";
	
    
    public static String request(String httpUrl, String httpArg) {  
        BufferedReader reader = null;  
        String result = null;  
        StringBuffer sbf = new StringBuffer();  
  
        try {  
            URL url = new URL(httpUrl);  
            HttpURLConnection connection = (HttpURLConnection) url  
                    .openConnection();  
            connection.setRequestMethod("POST");  
            connection.setRequestProperty("Content-Type",  
                    "application/x-www-form-urlencoded");  
            // 填入apikey到HTTP header  
            connection.setRequestProperty("apikey", "a9e80e65b4f94017a8317e877a14f34e");  
            connection.setDoOutput(true);  
            connection.getOutputStream().write(httpArg.getBytes("UTF-8"));  
            connection.connect();  
            InputStream is = connection.getInputStream();  
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));  
            String strRead = null;  
            while ((strRead = reader.readLine()) != null) {  
                sbf.append(strRead);  
                sbf.append("\r\n");  
            }  
            reader.close();  
            result = sbf.toString();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return result;  
    } 
    
    
	public static void main(String[] args) {
		
//		AipOcr aipOcr = new AipOcr(null, API_KEY, SECRET_KEY);
//		
//		aipOcr.setConnectionTimeoutInMillis(2000);
//		aipOcr.setSocketTimeoutInMillis(60000);
//		
//		String img = "D:\\code.jpg";
//		JSONObject genRes = aipOcr.general(img, new HashMap<String, String>());
//		System.out.println(genRes.toString());
		
//		Ocr.setUp();
//		Ocr ocr = new Ocr();
//		ocr.startEngine("eng", Ocr.SPEED_FASTEST);
//		String s = ocr.recognize(new File[] {new File("D:\\code.png")},
//				  Ocr.RECOGNIZE_TYPE_ALL, Ocr.OUTPUT_FORMAT_PLAINTEXT); // PLAINTEXT | XML | PDF | RTF
//		System.out.println("Result: " + s);
//		ocr.stopEngine();
		
		
//		 File file = new File("d:\\code.jpg");  
//	        String imageBase = BASE64.encodeImgageToBase64(file);  
//	        imageBase = imageBase.replaceAll("\r\n","");  
//	        imageBase = imageBase.replaceAll("\\+","%2B");  
//	        String httpUrl = "http://apis.baidu.com/apistore/idlocr/ocr";  
//	        String httpArg = "fromdevice=pc&clientip=10.10.10.0&detecttype=LocateRecognize&languagetype=CHN_ENG&imagetype=1&image="+imageBase;  
//	        String jsonResult = request(httpUrl, httpArg);  
//	        System.out.println("返回的结果--------->"+jsonResult);  
		
		
		File file = new File("D:\\code.jpg");
		ITesseract tesseract = new Tesseract();
		tesseract.setDatapath("D:\\Program Files (x86)\\Tesseract-OCR\\tessdata");  
		try {
			String result = tesseract.doOCR(file);
			System.out.println(result);
		} catch (TesseractException e) {
			e.printStackTrace();
		}
	}
}